/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.app.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import org.app.model.QueueURLs;
import org.app.service.CrawlsService;
import org.app.utils.Utils;
import org.gui.AutoDownloadMovieGUI;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 *
 * @author LamLe
 */
public class CrawlsServiceImpl implements CrawlsService {

    private static Logger logger = Logger.getLogger(CrawlsServiceImpl.class);
    AutoDownloadMovieGUI main;
    public final static String FSHARE = "href=\".{11}fshare.vn/file/\\.{10}/\"|href=\".{8}fshare.vn/file/\\.{10}/\"";

    public CrawlsServiceImpl() {
    }

    public CrawlsServiceImpl(AutoDownloadMovieGUI main) {
        this.main = main;
    }

    @Override
    public List<String> crawls(String url) throws Exception {
        List<String> urls = new ArrayList<String>();
        try {
        } catch (Exception e) {
            throw e;
        }
        return urls;
    }

    private void log(String str) {
        if (main != null) {
            main.write(str);
        } else {
            logger.info(str + "\n");
        }

    }

    @Override
    public void run() {
        try {
            while (QueueURLs.size() > 0) {
                try {
                    String url = QueueURLs.poll();
                    Connection conn = Jsoup.connect(url);
                    Utils.initRequestHeader(conn);
                    Document doc = conn.get();

                    String urls = Utils.filterString(doc);
                    if (!urls.equals("")) {
                        log("Matched from website : " + Utils.getDomain(url));
                        log("Real url : " + url);
                        log("----------------------------------------------");
                        log("----------------------------------------------");

                        log(urls);

                        log("End from website : " + Utils.getDomain(url));
                        log("----------------------------------------------");
                        log("----------------------------------------------");
                    }
                    logger.info("HTML Content : " + doc.html());

                } catch (Exception exception) {
                    logger.error("Failed", exception);
                }
                Thread.sleep(500);
            }
            log("");
            log("Done seach for " + main.getMovieName());
            log("--------------------------------------");
            log("--------------------------------------");
            log("");
            log("");
            main.unlock();
        } catch (Exception exception) {
            logger.error("Failed", exception);
        }
    }

    public static void main(String a[]) {
        new CrawlsServiceImpl().test();
    }

    private void start() {
        QueueURLs.add("http://suniso.vn/mrkingdom75/test/");
        run();
    }

    private void test() {
        logger.info("start");

        String str = "<p style=\"text-align:center;\"><a href=\"http://www.nhaccuatui.com/nghe?M=dtELHENGTCZ1\" target=\"“_blank”\">Episode 2: Vụ bắt cóc con gái tổng giám đốc</a></p>";

        // nhaccuatui
        String regex = "\"(.*)(nhaccuatui.com)\\/nghe\\?M=\\w{12}\\\"";

        // fshare
        // href="http://www.fshare.vn/file/TSXBNTP8YT/"
        regex = "href=\".{11}fshare.vn/file/\\w{10}/\"|href=\".{8}fshare.vn/file/\\w{10}/\"";


        str = "div><span><strong><a href=\"http://www.fshare.vn/file/TY5TS804JT\" target=\"_blank\"><span>http://www.fshare.vn/file/TY5TS804JT</span></a></strong></span></div>";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);

        int n = 1;
        while (matcher.find()) {
            logger.info("Link " + n++ + " : " + matcher.group());
        }
    }
}
